#!/usr/bin/env python
# -*- coding:utf-8 -*-
from conf.setting import *

def conn_mysql(sql):
    import pymysql
    try:
        conn = pymysql.connect(host=MYSQL_HOST, user=USER, password=PASSWORD, db=DB, charset='utf8', port=MYSQL_PORT)
    except Exception as e:
        print('mysql连接出错,错误信息为%s' % e)
        res = {"code": 9901, "msg": 'mysql连接出错,错误信息为%s' % e}
    else:
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    try:
        cur.execute(sql)
    except Exception as e:
        msg = "sql执行出错，请检查sql,错误信息为：%s" % e
        res = {"code": 9902, "msg": msg}
    else:
        res = cur.fetchone()
        conn.commit()
    finally:
        cur.close()
        conn.close()
        print("res:", res)
        return res
def md5_passwd(str):
    str = str+SALT  #加盐
    import hashlib
    md = hashlib.md5()
    md.update(str.encode)
    res = md.hexdigest()
    return res

def op_redis(k,v=None):
    import redis
    r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="")
    if v:
        r.setex(k,v,EX_TIME)
    else:
        return r.get(k)